package cn.easybuy.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;

import cn.easybuy.entity.ProductCategory;
import cn.easybuy.service.ProductCategoryService;
import cn.easybuy.service.impl.ProductCategoryServiceImpl;

/**
 * 商品分类Servlet
 */
@WebServlet("/ProductCategroyServlet")
public class ProductCategroyServlet extends HttpServlet {
	
	ProductCategoryService pcService = null;
	
	@Override
	public void init() throws ServletException {
		pcService = new ProductCategoryServiceImpl();
	}
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("进入了productCategory-----------------");
		response.setCharacterEncoding("utf-8");
		PrintWriter out= response.getWriter();
		
		String pid = request.getParameter("pid");
		System.out.println("pid:  "+pid);
		
		Integer _pid=0;
		//调用service方法,按id查询分类
		if ( pid!=null && !pid.equals("")) {
			_pid = Integer.valueOf(pid);
		}
		//得到二级分类
		List<ProductCategory> pc2_list= pcService.getProductCategroyByPid(_pid);
		
		//out.print(pc2_list);//不可以直接传给前端list集合 ,要转成json对象数组
		
		//怎么转,借助 aibaba 有个工具类 fastjson 
		JSONArray jsonArr =(JSONArray) JSON.toJSON(pc2_list);
		System.out.println("转换后的值:  "+jsonArr);
		out.print(jsonArr);
		
		
		out.flush();
		out.close();
		//out.print("success");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
